<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const articles = ref([])
const loading = ref(false)
const router = useRouter()

const fetchArticles = async () => {
    loading.value = true
    try {
        // Get latest articles
        const response = await fetch('http://127.0.0.1:8000/disease/message/0/0/0/?size=8')
        
        const data = await response.json()
        console.log(data.results.slice(0, 8) || [])
        articles.value = data.results?.slice(0, 8) || []
        
    } catch (error) {
        console.error('Error fetching articles:', error)
    } finally {
        loading.value = false
    }
}

const viewArticleNewPage = (articleId: number) => {
    const articleUrl = `/message/${articleId}`;
    window.open(articleUrl, '_blank');
}

onMounted(() => {
    fetchArticles()
})
</script>

<template>
    <div class="article-container">
        <div class="article-header">
            <h2 class="article-title">科普文章</h2>
            <router-link to="/disease/1" class="article-more">查看更多</router-link>
        </div>

        <div class="article-grid">
            <div class="article-column">
                <!-- First article with cover -->
                <div v-if="articles[0]" class="article-featured" @click="viewArticleNewPage(articles[0].id)">
                    <img :src="articles[0].cover" class="featured-image" alt="文章封面">
                    <h3 class="featured-title">{{ articles[0].name}}</h3>
                    <p class="featured-content" v-html="articles[0].content.slice(0, 30)+'...'"></p>
                </div>
                <!-- Other articles in first column -->
                <div v-for="article in articles.slice(1, 4)" :key="article.id" class="article-item"
                    @click="viewArticleNewPage(article.id)">
                    <h4 class="article-item-title">{{ article.name }}</h4>
                </div>
            </div>

            <div class="article-column">
                <!-- First article with cover -->
                <div v-if="articles[4]" class="article-featured" @click="viewArticleNewPage(articles[4].id)">
                    <img :src="articles[4].cover" class="featured-image" alt="文章封面">
                    <h3 class="featured-title">{{ articles[4].name }}</h3>
                    <p class="featured-content" v-html="articles[4].content.slice(0, 30) + '...'"></p>
                </div>
                <!-- Other articles in second column -->
                <div v-for="article in articles.slice(5, 8)" :key="article.id" class="article-item"
                    @click="viewArticleNewPage(article.id)">
                    <h4 class="article-item-title">{{ article.name }}</h4>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.article-container {
    padding: 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    max-width: 1270px;
    margin: 20px auto;
}

.article-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.article-title {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

.article-more {
    color: #00b38a;
    text-decoration: none;
}

.article-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
}

.article-column {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.article-featured {
    padding: 15px;
    border: 1px solid #eee;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.article-featured:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.featured-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
    border-radius: 4px;
    margin-bottom: 10px;
}

.featured-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 8px;
}

.featured-content {
    font-size: 14px;
    color: #666;
    line-height: 1.5;
}

.article-item {
    padding: 12px;
    border: 1px solid #eee;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.article-item:hover {
    background-color: #f9f9f9;
}

.article-item-title {
    font-size: 14px;
    color: #333;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>